﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using System.Text.RegularExpressions;
using Petroleum.Model;
using Petroleum.UC.Public;
using HZH_Controls.Forms;
using System.Data.SQLite;
using Newtonsoft.Json;

namespace Petroleum.UC
{
    public partial class Step11 : UserControl
    {
        FormMain main = new FormMain();
        /// <summary>
        /// 套管List
        /// </summary>
        List<Casing> casingList = new List<Casing>();
        bool hassave = false;
        /// <summary>
        /// 当前操作对象
        /// </summary>
        Casing casing = new Casing();
        List<MPA> MPAs = new List<MPA>();
        public List<MPA> Type35 = new List<MPA>()
        {
            new MPA(){ Type ="FH35-35",WellControlEquipment="Annular Preventer",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="2FZ35-35",WellControlEquipment="Double Ram Preventer",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="JG-35",WellControlEquipment="Choke manifold",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="YG-35",WellControlEquipment="Kill manifold",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="FKQ800-7H",WellControlEquipment="control device line",Medium="hydraulic oil",TestPressureMPa="21",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
        };
        public List<MPA> Type38 = new List<MPA>()
        {
            new MPA(){ Type="FH35-35-38",WellControlEquipment="Annular Preventer",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="2FZ35-35-38",WellControlEquipment="Double Ram Preventer",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="JG-35-38",WellControlEquipment="Choke manifold",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="YG-35-38",WellControlEquipment="Kill manifold",Medium="Clean Water",TestPressureMPa="17",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
            new MPA(){ Type="FKQ800-7H-38",WellControlEquipment="control device line",Medium="hydraulic oil",TestPressureMPa="21",HoldingTimeMin="≥10",AllowablePressureDropMpa="≤0.7",Sealingparts="No seepage"},
        };
        public Step11(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            Load();
        }
        void Load()
        {
            casingList = Session.CasingProgram.CasingList;
            List<KeyValuePair<string, string>> spudCom = casingList.Select(m =>
                new KeyValuePair<string, string>(m.SpudNo, m.SpudNo)
            ).ToList();
            cmdCasing.Source = spudCom;
            cmdCasing.SelectedIndex = 0;
            List<KeyValuePair<string, string>> lstCom = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("35mm","35mm"),
                new KeyValuePair<string, string>("38mm","38mm"),
            };
            cmdType.Source = lstCom;
            cmdType.SelectedIndex = 0;
            MPAs = Type35;
        }
        private void ucDataGridView1_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "WellControlEquipment", HeadText = "WellControlEquipment", Width = 300, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 160, DataField = "Type", HeadText = "Type", WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 160, DataField = "Medium", HeadText = "Medium", WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 160, DataField = "TestPressureMPa", HeadText = "TestPressureMPa", WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 160, DataField = "HoldingTimeMin", HeadText = "HoldingTimeMin", WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 160, DataField = "AllowablePressureDropMpa", HeadText = "AllowablePressureDropMpa", WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { Width = 160, DataField = "Sealingparts", HeadText = "Sealingparts", WidthType = SizeType.Absolute });
            this.dgv.Columns = lstCulumns;
            this.dgv.DataSource = MPAs;
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            hassave = true;
            main.AddControl(new Step12(main));
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step10(main));
        }
        private void cmdCasing_SelectedChangedEvent(object sender, EventArgs e)
        {
            //获取对应的套管
            string spud = cmdCasing.SelectedValue;
            casing = casingList.FirstOrDefault(m => m.SpudNo == spud);
        }
        private void cmdType_SelectedChangedEvent(object sender, EventArgs e)
        {
            switch (cmdType.SelectedValue)
            {
                case "35mm":
                    MPAs = Type35;
                    break;
                case "38mm":
                    MPAs = Type38;
                    break;
                default:
                    break;
            }
            dgv.DataSource = MPAs;
        }
        private void btnedit_BtnClick(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                MPA target = new MPA();
                int row = dgv.SelectRow.RowIndex;
                target = MPAs[row];
                FrmInputs frm = new FrmInputs($"正在编辑{target.WellControlEquipment}",
                             new string[] { "WellControlEquipment", "Type", "Medium", "TestPressureMPa", "HoldingTimeMin", "AllowablePressureDropMpa", "Sealingparts" }, null,
                             //new Dictionary<string, HZH_Controls.TextInputType>() { { "Easting", HZH_Controls.TextInputType.Number }, { "Northing", HZH_Controls.TextInputType.Number }, { "ErrorRadius", HZH_Controls.TextInputType.Number } },
                             new Dictionary<string, string>() { }, null,
                             //new Dictionary<string, KeyBoardType>() { { "Easting", KeyBoardType.Null }, { "Northing", KeyBoardType.UCKeyBorderNum }, { "ErrorRadius", KeyBoardType.UCKeyBorderNum } },
                             new List<string>() { "WellControlEquipment", "Type", "Medium", "WellControlEquipment", "TestPressureMPa", "HoldingTimeMin", "AllowablePressureDropMpa", "Sealingparts" }, new Dictionary<string, string> {
                         { "WellControlEquipment",target.WellControlEquipment },
                         { "Type",target.Type },
                         { "Medium",target.Medium },
                         { "TestPressureMPa",target.TestPressureMPa},
                         { "HoldingTimeMin",target.HoldingTimeMin},
                         { "AllowablePressureDropMpa",target.AllowablePressureDropMpa},
                         { "Sealingparts",target.Sealingparts},
                             });
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    target.WellControlEquipment = frm.Values[0];
                    target.Type = frm.Values[1];
                    target.Medium = frm.Values[2];
                    target.TestPressureMPa = frm.Values[3];
                    target.HoldingTimeMin = frm.Values[4];
                    target.AllowablePressureDropMpa = frm.Values[5];
                    target.Sealingparts = frm.Values[6];
                    this.dgv.DataSource = MPAs;
                    FrmTips.ShowTipsInfo(this.FindForm(), $"已将{target.WellControlEquipment}的值更新");
                }
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要编辑的行");
        }
        private void btndel_BtnClick(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                MPAs.RemoveAt(row);
                this.dgv.DataSource = MPAs;
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        private void btnadd_BtnClick(object sender, EventArgs e)
        {
            FrmInputs frm = new FrmInputs($"添加",
                            new string[] { "WellControlEquipment", "Type", "Medium", "TestPressureMPa", "HoldingTimeMin", "AllowablePressureDropMpa", "Sealingparts" }, null,
                            //new Dictionary<string, HZH_Controls.TextInputType>() { { "Easting", HZH_Controls.TextInputType.Number }, { "Northing", HZH_Controls.TextInputType.Number }, { "ErrorRadius", HZH_Controls.TextInputType.Number } },
                            new Dictionary<string, string>() { }, null,
                            //new Dictionary<string, KeyBoardType>() { { "Easting", KeyBoardType.Null }, { "Northing", KeyBoardType.UCKeyBorderNum }, { "ErrorRadius", KeyBoardType.UCKeyBorderNum } },
                            new List<string>() { "WellControlEquipment", "Type", "Medium", "WellControlEquipment", "TestPressureMPa", "HoldingTimeMin", "AllowablePressureDropMpa", "Sealingparts" }, new Dictionary<string, string> {
                          { "WellControlEquipment","" },
                         { "Type","" },
                         { "Medium","" },
                         { "TestPressureMPa",""},
                         { "HoldingTimeMin",""},
                         { "AllowablePressureDropMpa",""},
                         { "Sealingparts",""},
                            });
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                MPA target = new MPA();
                target.WellControlEquipment = frm.Values[0];
                target.Type = frm.Values[1];
                target.Medium = frm.Values[2];
                target.TestPressureMPa = frm.Values[3];
                target.HoldingTimeMin = frm.Values[4];
                target.AllowablePressureDropMpa = frm.Values[5];
                target.Sealingparts = frm.Values[6];
                MPAs.Add(target);
                this.dgv.DataSource = MPAs;
                FrmTips.ShowTipsInfo(this.FindForm(), $"添加成功");
            }
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var casing = BaseModel.GetTableData(main.projects, TableNames.MPAs.ToString());
            if (casing != null)
            {
                Session.MPAList = JsonConvert.DeserializeObject<List<MPA>>(casing);
                this.dgv.DataSource = Session.MPAList;
            }
            var MPAdetail = BaseModel.GetTableData(main.projects, TableNames.MPAdetail.ToString());
            if (MPAdetail != null)
            {
                Session.MPADetailList = JsonConvert.DeserializeObject<List<MPADetail>>(MPAdetail);
                int i = 0;
                foreach (var item in Session.MPADetailList)
                {
                    if (i == 0)
                    {
                        txtallowed.InputText = item.Allowedpressure;
                        txtcgsize.InputText = item.CsgSize;
                        txtmedium.InputText = item.Medium;
                        txttest.InputText = item.TestPressure;
                        txtholding.InputText = item.Holdingtime;
                        txtremark.Text = item.Remarks;
                    }
                    else
                    {
                        txtallowed1.InputText = item.Allowedpressure;
                        txtcgsize1.InputText = item.CsgSize;
                        txtmedium1.InputText = item.Medium;
                        txttest1.InputText = item.TestPressure;
                        txtholding1.InputText = item.Holdingtime;
                    }
                    i++;
                }
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            if (!hassave)
            {
                SaveData();
            }
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var mpas = projects.Where(n => n.TableName == TableNames.MPAs.ToString()).FirstOrDefault();
            if (mpas != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.MPAs.ToString(), JsonConvert.SerializeObject(Session.MPAList));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.MPAs.ToString(), JsonConvert.SerializeObject(Session.MPAList), 1);

            var mpasdetail = projects.Where(n => n.TableName == TableNames.MPAdetail.ToString()).FirstOrDefault();
            if (mpasdetail != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.MPAdetail.ToString(), JsonConvert.SerializeObject(Session.MPADetailList));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.MPAdetail.ToString(), JsonConvert.SerializeObject(Session.MPADetailList), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        void SaveData()
        {
            if (MPAs.Count > 0)
            {
                MPAs[0].SpudNo = cmdCasing.SelectedText;
            }
            Session.MPAList = MPAs;

            List<MPADetail> pADetails = new List<MPADetail>() {
                new MPADetail(){CsgSize=txtcgsize.InputText,
                Allowedpressure=txtallowed.InputText,
                Medium=txtmedium.InputText,
                TestPressure=txttest.InputText,
                Holdingtime=txtholding.InputText,
                Remarks=txtremark.Text,
                },
                new MPADetail(){CsgSize=txtcgsize1.InputText,
                Allowedpressure=txtallowed1.InputText,
                Medium=txtmedium1.InputText,
                TestPressure=txttest1.InputText,
                Holdingtime=txtholding1.InputText,
                },
            };
            Session.MPADetailList = pADetails;
        }
    }
}
